﻿using ALMForCRMToolkit.Models;
using ALMForCRMToolkit.Repository;
using Microsoft.AspNet.Identity;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ALMForCRMToolkit.CoreLogic
{
    public class LockRuleManager : IDisposable
    {
        LockRuleRepository _lockRuleRepository = null;

        public LockRuleManager()
        {
            _lockRuleRepository = new LockRuleRepository();
        }
        /// <summary>
        /// Checks if there any Lock Rules that conflict with the current Lock Rule
        /// </summary>
        /// <param name="currentLockRuleId">Current Lock Rule ID</param>
        void CheckLockRuleConflicts()
        {

        }

        void CreateLockRule(LockRule rule)
        {
            if (rule != null)
            {

            }

            //Retrieve All of the Active/Published Lock Rules and 
            //Determine if this new Rule does not conflict with any rule

            var rules = _lockRuleRepository.Query(x => x.Status == EnumTypes.LockRuleStatus.Published && x.SolutionId == rule.SolutionId);

            if (rules.Count() > 0)
            {
                throw new ApplicationException("Duplicate Lock Rule Found: There is a Lock Rule Published for the current Solution");
            }

            //Create Record
            _lockRuleRepository.Create(rule);
        }

        #region Dispose
        private bool disposed = false;

        protected virtual void Dispose(bool disposing)
        {
            if (!this.disposed)
            {
                if (disposing)
                {
                }
            }
            this.disposed = true;
        }

        public void Dispose()
        {
            Dispose(true);
            GC.SuppressFinalize(this);
        }

        #endregion
    }
}
